package com.zls.service.impl;

import com.zls.dto.DeveloperDto;
import com.zls.mapper.DeveloperMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.pojo.Developer;
import com.zls.pojo.Project;
import com.zls.service.DeveloperService;
import com.zls.service.ProjectService;
import com.zls.utils.UUIDUtils;
import com.zls.vo.DevProVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DeveloperServiceImpl implements DeveloperService {
    @Autowired
    private DeveloperMapper developerMapper;
    @Autowired
    private ProjectService projectService;
    @Override
    public List<Developer> findAll() {
        return developerMapper.findAll();
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        String queryString = queryPageBean.getQueryString();//查询条件
        //total总记录数
        List<Developer> page = developerMapper.findDeve((currentPage - 1) * pageSize, pageSize,queryString);
        //rows  当前页的数据
        Long count = developerMapper.count(queryString);
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    @Transactional
    //添加开发商，将工程进行绑定
    public int addAndBind(String dname, int[] pids) {
        Developer developer=new Developer();
        developer.setDid(UUIDUtils.uuid());
        developer.setDname(dname);
        developer.setUpdatetime(new Date());
        int add = developerMapper.add(developer);
        //绑定工程
        projectService.bindDid(developer.getDid(),pids);
        return add;
    }

    //删除逻辑：删除开发商的时候，同时要将工程对应的开发商的id设置为null
    @Override
    @Transactional
    public int deleteDeve(String did) {
        projectService.deleteByDid(did);
        int i = developerMapper.deleteDeve(did);
        return i;

    }

    //did查找修改
    @Override
    public DeveloperDto findByDid(String did) {
        DeveloperDto developerDto = developerMapper.findByDidDto(did);
        List<Project> list = projectService.findByDids(did);
        int[] pids=null;
        if (list!=null){
            pids=new int[list.size()];
            for (int i = 0; i <list.size() ; i++) {
                pids[i]=list.get(i).getPid();
            }
            developerDto.setPids(pids);
        }
        return developerDto;
    }

    //修改
    @Transactional
    @Override
    public int editDeve(DevProVo devProVo) {
        devProVo.setUpdatetime(new Date());
        int i = developerMapper.updateDeve(devProVo);
        //同时更新project的did
        projectService.deleteByDid(devProVo.getDid());
        //绑定工程
        projectService.bindDid(devProVo.getDid(),devProVo.getPids());
        return i;
    }
}
